package cn.shoukaiseki.sjf4j.node;


import com.fasterxml.jackson.databind.util.ClassUtil;

import java.util.Iterator;

public abstract class BaseTreeNode<T> implements TreeNode {

    @Override
    public abstract JsonNodeType getNodeType();

    @Override
    public final boolean isValueNode()
    {
        switch (getNodeType()) {
            case ARRAY: case OBJECT: case MISSING:
                return false;
            default:
                return true;
        }
    }

    @Override
    public boolean isContainerNode() {
        final JsonNodeType type = getNodeType();
        return type == JsonNodeType.OBJECT || type == JsonNodeType.ARRAY;
    }

    @Override
    public boolean isMissingNode() {
        final JsonNodeType type = getNodeType();
        return type == JsonNodeType.MISSING;
    }

    @Override
    public boolean isObject() {
        final JsonNodeType type = getNodeType();
        return type == JsonNodeType.OBJECT;
    }

    @Override
    public boolean isArray() {
        final JsonNodeType type = getNodeType();
        return type == JsonNodeType.ARRAY;
    }

    @Override
    public Iterator<String> fieldNames() {
        return ClassUtil.emptyIterator();
    }

    @Override
    public final boolean isNumber() {
        return getNodeType() == JsonNodeType.NUMBER;
    }

    @Override
    public boolean isNull() {
        return getNodeType() == JsonNodeType.NULL;
    }

    @Override
    public boolean isTextual() {
        return getNodeType()==JsonNodeType.STRING;
    }

    @Override
    public boolean isBigDecimal() {
        return getNodeType()==JsonNodeType.NUMBER&&numberType()==NumberType.BIG_DECIMAL;
    }

    @Override
    public String textValue() {
        return null;
    }

    @Override
    public Double asDoubleVal() {
        if (getNodeType()== JsonNodeType.NULL){return null;}
        return asDouble();
    }

    @Override
    public Boolean asBooleanVal() {
        if (getNodeType()== JsonNodeType.NULL){return null;}
        return asBoolean();
    }

    @Override
    public Integer asIntVal() {
        if (getNodeType()== JsonNodeType.NULL){return null;}
        return asInt();
    }

    @Override
    public Long asLongVal() {
        if (getNodeType()== JsonNodeType.NULL){return null;}
        return asLong();
    }


}
